<?php
require 'db_config.php';
require 'open_db.php';
require_once 'includes/resources.php';
include 'includes/recipes.inc';
include 'includes/units.inc';
include 'includes/ingredients.inc';
$recipes = array();


$page = 0;
$query = "SELECT r_id FROM recipe";
if(isset($_GET['q'])){
  $node = safe($_GET['q']);
  $query .= " WHERE r_id = {$node} ";
  $page = $node;
} 

$qid = mysql_query($query);
while($row= mysql_fetch_object($qid)){
    array_push($recipes,get_recipe($row -> r_id));
}

include 'includes/header.php';
?>

<body>
    <?php foreach($recipes as $recipe){?>
        <div class="node" id="node_<?php echo $recipe -> r_id; ?>">
            <h2><?php 
            if($page == 0){
              echo "<a href=\"?q=" . $recipe -> r_id . "\">" . $recipe -> r_name . "</a>";
            }else{
            echo $recipe -> r_name;
            } ?></h2>
            <div class="ingredients">
                <h3>Ingredients</h3>
                <ul>
                    <?php
                    $i = 0;
                    foreach($recipe -> ingredients as $ing){
                        echo "<li id=" . $recipe -> r_id . "_ing_" . $i .">";
                        echo $ing -> ing_q . " ";
                        if($ing -> unit_s != "N/A"){
                          echo print_unit($ing -> ing_id, $ing -> ing_q);
                        }
                        echo " ";
                        if($ing -> mod_name != "NULL"){
                          echo $ing -> mod_name;
                        }
                        echo " ";
                        
                        echo $ing -> ing_name;
                        echo "</li>\n";
                        $i++;
                    }
                    ?>
                </ul>
                <div class="content">
                    <h3>Instructions</h3>
                    <?php echo $recipe -> r_content . "\n"; ?>
                </div>
            </div>
        <hr/>
    <?php } ?>
    </div>
</body>
</html>
